package org.liang.auth.controller;

import io.swagger.annotations.ApiOperation;
import org.liang.auth.service.IPermissionService;
import org.liang.auth.domain.Permission;
import org.liang.auth.query.PermissionQuery;
import org.liang.base.annotation.RonghuaPermission;
import org.liang.base.utils.AjaxResult;
import org.liang.base.utils.PageInfo;
import org.liang.system.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/permission")
@RonghuaPermission(name = "权限管理",desc = "权限相关接口")
public class PermissionController {
    @Autowired
    public IPermissionService permissionService;

    /**
     * 保存和修改公用的
     * @param permission  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @RonghuaPermission(name = "权限保存",desc = "权限新增和修改")
    public AjaxResult save(@RequestBody Permission permission){
        try {
            if( permission.getId()!=null)
                permissionService.update(permission);
            else
                permissionService.insert(permission);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @RonghuaPermission(name = "权限删除",desc = "权限根据id删除")
    public AjaxResult delete(@PathVariable("id") Serializable id){
        try {
            permissionService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
    * 删除所有对象信息
    * @param ids
    * @return
    */
    @PatchMapping
    @RonghuaPermission(name = "权限批量删除",desc = "权限批量删除")
    public AjaxResult batchDelete(@RequestBody List<Serializable> ids){
        try {
            permissionService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @RonghuaPermission(name = "权限查询",desc = "权限根据id查询")
    public AjaxResult selectById(@PathVariable("id")Serializable id)
    {
        try {
            Permission permission = permissionService.selectById(id);
            return AjaxResult.success(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    @RonghuaPermission(name = "权限查询",desc = "权限查询所有")
    public AjaxResult selectAll(){
        try {
            List< Permission> list = permissionService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @RonghuaPermission(name = "权限分页",desc = "权限分页查询")
    public AjaxResult queryPage(@RequestBody PermissionQuery query)
    {
        try {
            PageInfo pageInfo = permissionService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    // 查部门上下级
    @ApiOperation(value = "查询权限树")
    @GetMapping("/perTree")
    public AjaxResult perTree(){
        try {
            List<Permission> perTree = permissionService.perTree();
            return AjaxResult.success(perTree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}
